﻿using AutoMapper;
using MediatR;
using S.Organization.API.Application.Command.CottageCommand;
using S.Organization.Domian.Model;
using S.Organization.Infrastructure;

namespace S.Organization.API.Application.CommandHandler.CottageCommandHandler
{
    public class UpdCottageCommandHandler:IRequestHandler<UpdCottageCommand,int>
    {
        private readonly IBaseRepository<CottageSetting> _repository;
        private readonly IMapper mapper;

        public UpdCottageCommandHandler(IBaseRepository<CottageSetting> repository, IMapper mapper)
        {
            _repository = repository;
            this.mapper = mapper;
        }

        public async Task<int> Handle(UpdCottageCommand request, CancellationToken cancellationToken)
        {
            var list = await _repository.GetAllAsync();
            var list1 = list.Where(x => x.CottageId == request.CottageId).FirstOrDefault();
            request.CreateDate = list1.CreateDate;
            request.CreateBy = list1.CreateBy;
            request.UpdateBy = "李四";
            request.UpdateDate = DateTime.Now;
            return await _repository.UpdateAsync(mapper.Map<CottageSetting>(request));
        }
    }
}
